package de.sabram.upb.specs.eee_tools.contextMenuActions;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

import de.sabram.upb.specs.eee_tools.Activator;
import de.sabram.upb.specs.eee_tools.EliRuntimePreferencePage;

public class GenerateCompiler implements IViewActionDelegate{

	public GenerateCompiler() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void run(IAction action) {
		MessageConsole console = new MessageConsole("Eli Output", null);
		console.activate();
		ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{ console });
		MessageConsoleStream stream = console.newMessageStream();
		try {
			 // setup working directory
			IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
			IProject currentProject = null;
			IProject [] projects = workspaceRoot.getProjects();
			for(IProject project : projects){
				if(project.exists() && project.isOpen())
					currentProject = project;
			}
			currentProject.getName().toString();
			if (currentProject.findMember(currentProject.getName() + ".specs") == null){
				throw new NoSpecsFileException();
			}
			// run eli
			ProcessBuilder pb = new ProcessBuilder
					(
					Activator.getDefault().getPreferenceStore().getString("ELI_PATH").toString()+"/eli", 
					Activator.getDefault().getPreferenceStore().getString("CHOICE") ,
					currentProject.getName().toString()+".specs:exe>."
					);
			
			IPath pathToProject = currentProject.getLocation();
			File dir = pathToProject.toFile();
			pb.directory(dir);
			Process p = pb.start();
			p.waitFor();
			BufferedReader buf = new BufferedReader( new InputStreamReader( p.getInputStream() ) ) ;
			String line;
			while ((line = buf.readLine())!=null)
				stream.println(line);
		} catch (IOException e) {
			stream.println("ERROR could not find Eli");
			
		} catch (InterruptedException e) {
			stream.println("Error Eli was interrupted");
			
		} catch(NoSpecsFileException e){
			stream.println("ERROR no specs File has been defined");
		}
		
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {	
	}

	@Override
	public void init(IViewPart view) {
	}

}
